

	var id = GetQueryString("id");
	var ztree;
	var setting = {
		    data: {
		        simpleData: {
		            enable: true,
		            idKey: "id",
		            pIdKey: "parentId",
		            rootPId: -1
		        },
		        key: {
		            url:"nourl"
		        }
		    }
		};
	
	var vm = new Vue({
    	  el: '#app',
    	  data: {
    		  menu:{
    	            parentName:null,
    	            parentId:0,
    	            type:1,
    	            orderNum:0
    	        }
    	  },
    	  methods: {
    		  // 获取指定菜单
    		  getMenu: function(id){
    	        	var url = baseURL + "sys/menu/info/" + id;
    	    		$.get(url, function(data){
    	    			vm.menu = data.menu;
    	    			vm.getMenuTree();
    	    		});
    	      },
    	      getMenuTree: function(){
    	            //加载菜单树
    	            $.get(baseURL + "sys/menu/select", function(r){
    	                ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
    	                var node = ztree.getNodeByParam("id", vm.menu.parentId);
    	                ztree.selectNode(node);
    	                vm.menu.parentName = node.name;
    	            })
    	        },
    	        menuTree: function(){
    	        	
    	            layer.open({
    	                type: 1,
    	                title: "选择菜单",
    	                area: ['300px', '360px'],
    	                shade: 0,
    	                shadeClose: false,
    	                content: jQuery("#menuLayer"),
    	                btn: ['确定', '取消'],
    	                btn1: function (index) {
    	                    var node = ztree.getSelectedNodes();
    	                    //选择上级菜单
    	                    vm.menu.parentId = node[0].id;
    	                    vm.menu.parentName = node[0].name;

    	                    layer.close(index);
    	                }
    	            });
    	      },
    	      saveOrUpdate: function () {
    	            var url = vm.menu.id == null ? "sys/menu/save" : "sys/menu/update";
    	            $.loadAjax({
    	                type: "POST",
    	                url: baseURL + url,
    	                contentType: "application/json",
    	                data: JSON.stringify(vm.menu),
    	                success: function(r){
    	                    if(r.code === 0){
    	                        layer.alert('提交成功', function(){
    	                            parent.location.reload();
    	                        });
    	                    }else{
    	                        alert(r.msg);
    	                    }
    	                }
    	            });
    	      }
    	  },
    	  mounted:function(){
              	
    		// id 存在则为编辑
		  	if (id != null && id.length > 0) {
		  		this.getMenu(id);
		  	} else {
		  		this.getMenuTree();
		  	}
    	  }
    });

		
	//表单验证
	$(function() {

        $("#editForm").validate({
            rules: {
                name: "required",
                parentName: "required",
                url:{
                	emptyUrl: true
                }
                
            },
            messages: {
            	name: "请输入菜单名",
            	parentName: "请输入上级菜单"
            },
            submitHandler : function(form) {  //验证通过后的执行方法
                //当前的form通过ajax方式提交
                vm.saveOrUpdate();
            },
            /* 重写错误显示消息方法,以tips方式弹出错误消息 */   
            showErrors: function(errorMap, errorList) {   
            	var msg = "";   
                $.each( errorList, function(i,v){   
                  msg = (v.message+"\r\n");   
                  if(msg!="") layer.tips(msg, "#"+v.element.id, { tipsMore: true });   
                });   
                // if(msg!="") layer.alert(msg);   
            },   
            /* 提示失去焦点时不验证 */    
            onfocusout: false 
        });
    });
	
	/**** 自定义验证规则 ****/
	
	// 检验菜单url
	jQuery.validator.addMethod("emptyUrl", function(value, element) {
        
		var flag = true;
		if ($('input[name="type"]:checked').val() == 1){
			if (value == null || value.length == 0) {
				flag = false;
			}
		}
        return flag;
    }, "菜单URL不能为空");
    
        
        